<script lang="ts" setup>
  import { ref, watch, onMounted, onUnmounted } from 'vue';
  import MEcharts from '/@/components/MEcharts/MEcharts.vue';
  import * as echarts from 'echarts/core';
  const props = defineProps({
    actData: Array<any>,
    xdata: Array<any>,
    seriesName: String,
  });

  const chartRef = ref();
  const option = ref<any>({
    grid: {
      top: '5%',
      left: '5%',
      right: '5%',
      bottom: '16%',
    },
    // animation: false,
    tooltip: {
      trigger: 'axis',
      axisPointer: { type: 'shadow' }, //阴影
    },
    xAxis: {
      data: [],
      type: 'category',
      axisLabel: {
        color: '#fff',
      },
      axisTick: {
        show: false,
      },
      axisLine: {
        show: true,
      },
      z: 10,
    },
    yAxis: {
      color: '#fff',
      name: 'mm',
      nameTextStyle: {
        color: '#fff',
        fontSize: 12,
      },
      boundaryGap: ['0', '10%'],
      axisLine: {
        show: true,
      },
      axisTick: {
        show: true,
      },
      axisLabel: {
        color: '#fff',
      },
      splitLine: {
        show: false,
      },
      scale: true,
    },
    visualMap: {
      show: false,
      pieces: [
        {
          gte: 0,
          lte: 1000,
          color: '#FD0100',
        },
      ],
      outOfRange: {
        color: '#00FF00',
      },
    },
    series: [
      {
        type: 'line',
        name: 'AI实时温度曲线',
        data: [],
        lineStyle: {
          width: 1,
          color: '#00baff',
        },
        itemStyle: {},
        force: {
          layoutAnimation: false,
        },
        symbolSize: 1,
        smooth: false, //曲线or折线
        areaStyle: {
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            { offset: 0, color: 'rgba(0, 186, 255, 0.6)' },
            { offset: 1, color: 'rgba(0, 186, 255, 0.1)' },
          ]),
        },
      },
      {
        type: 'line',
        name: 'MM实时温度曲线',
        data: [],
        lineStyle: {
          width: 1,
          color: '#f4c74f',
        },
        itemStyle: {},
        force: {
          layoutAnimation: false,
        },
        symbolSize: 1,
        smooth: false,
        areaStyle: {
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            { offset: 0, color: 'rgba(244, 199, 79, 0.5)' },
            { offset: 1, color: 'rgba(244, 199, 79, 0.1)' },
          ]),
        },
      },
      {
        type: 'line',
        name: 'PLC实时温度曲线',
        data: [],
        lineStyle: {
          width: 1,
          color: '#2af598',
        },
        itemStyle: {},
        force: {
          layoutAnimation: false,
        },
        symbolSize: 1,
        smooth: false,
        areaStyle: {
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            { offset: 0, color: 'rgba(42, 245, 152, 0.5)' },
            { offset: 1, color: 'rgba(42, 245, 152, 0.1)' },
          ]),
        },
      },
    ],
  });
  const myChart = ref<InstanceType<typeof MEcharts>>();

  const updateChart = () => {
    option.value.xAxis.data = props.xdata;
    option.value.series[0].data = props.actData;

    const data1 = props.actData ?? [0];
    option.value.yAxis.min = Math.min(...data1) - 1;
    option.value.yAxis.max = Math.max(...data1) + 1;

    myChart.value?.resizeHandler();
  };

  watch(
    () => {
      return props.actData?.length;
    },
    () => {
      updateChart();
    }
  );

  onMounted(() => {
    // 确保在 DOM 加载完成后更新图表
    setTimeout(() => {
      updateChart();
    }, 100);

    // 添加窗口大小变化监听
    window.addEventListener('resize', updateChart);
  });

  onUnmounted(() => {
    // 移除窗口大小变化监听
    window.removeEventListener('resize', updateChart);
  });

  defineExpose({
    updateChart, // 暴露方法到父组件
    chartRef,
  });
</script>

<template>
  <div ref="chartRef" class="chart-container">
    <m-echarts :option="option"></m-echarts>
  </div>
</template>

<style scoped>
  .chart-container {
    width: 100%;
    height: 100%;
    min-height: 11vh;
  }
</style>
